//
//  KPFormViewController.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/4/5.
//  Copyright © 2018年 Kipp. All rights reserved.
//


import UIKit
import Eureka
import TangramKit
import IQKeyboardManagerSwift
class KPFormViewController: FormViewController {

    typealias ConfirmClosure = ((_ row: BaseRow) -> Void)

    var selectRow: BaseRow?
    var confirm: ConfirmClosure?


    var rootLayout: TGRelativeLayout!


    override func viewDidLoad() {
        super.viewDidLoad()
        config()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        IQKeyboardManager.sharedManager().enable = false
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        IQKeyboardManager.sharedManager().enable = false
    }

    override func loadView() {
        rootLayout = TGRelativeLayout()
        rootLayout.bounds = Screen.bounds
        view = rootLayout
    }


    fileprivate func config() {
        navigationAccessoryView = KPNavigationAccessoryView().then {
            $0.confirmButton.action = #selector(confirmAction(_:))
        }
        view.backgroundColor = #colorLiteral(red: 0.9490196078, green: 0.9490196078, blue: 0.9490196078, alpha: 1)
        kp_setNavigationBarColor(color: UIColor.Material.white, tintColor: UIColor.Material.black)
        //如果格式验证错误，默认显示title为红色，全局配置
        TextRow.defaultCellUpdate = { cell, row in
            if !row.isValid {
                cell.titleLabel?.textColor = .red
            }
        }




    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }

    @objc
    open func confirmAction(_ sender: UIBarButtonItem) {
        if let closure = confirm, let selectRow = selectRow {
            closure(selectRow)
        }
        tableView.endEditing(true)
    }

    override func inputAccessoryView(for row: BaseRow) -> UIView? {
        selectRow = row
        return super.inputAccessoryView(for: row)
    }

    override func rt_customBackItem(withTarget target: Any!, action: Selector!) -> UIBarButtonItem! {
        return UIBarButtonItem.init(image: #imageLiteral(resourceName: "返回"), style: .plain, target: target, action: action)
    }


    /// 获取一个 section header 、footer view
    ///
    /// - Parameter height: 高度
    /// - Returns: 返回一个 HeaderFooterView，默认为空
    func getEmptySectionView(height: Double = 0) -> HeaderFooterView<UIView> {
        var header = HeaderFooterView<UIView>(.callback({
            let view = UIView(frame: CGRect.init(x: 0, y: 0, width: Screen.width, height: height.cgFloat))
            return view
        }))
        header.height = { height.cgFloat }
        return header
    }

}
